package bscorp.appbase.bmo;

import android.util.Log;

import java.sql.SQLException;
import java.util.List;

import bscorp.appbase.AppManager;
import bscorp.appbase.db.M4Dao;
import bscorp.appbase.model.Device;

public class DeviceBmo {
    private static String TAG = "DeviceBmo";

    /**
     * 保存设备信息
     *
     * @param model
     */
    public boolean saveDevice(Device model) {
        try {
            M4Dao.getInstance(AppManager.context).add(model, Device.class);
            return true;
        } catch (SQLException e) {
            Log.e("jamie", "---saveDevice error---");
            return false;
        }
    }

    /**
     * 修改设备信息
     *
     * @param model
     */
    public boolean updateDevice(Device model) {
        try {
            M4Dao.getInstance(AppManager.context).update(model, Device.class);
            return true;
        } catch (SQLException e) {
            Log.e("jamie", "---saveDevice error---");
            return false;
        }
    }

    /**
     * 修改设备信息
     *
     * @param model
     */
    public boolean deleteDevice(Device model) {
        try {
            M4Dao.getInstance(AppManager.context).deleteById(model.getId(), Device.class);
            return true;
        } catch (SQLException e) {
            Log.e("jamie", "---saveDevice error---");
            return false;
        }
    }

    /**
     * 通过Id获取设备
     *
     * @return
     */
    public Device getDeviceById(int id) {
        try {
            Device device = (Device) M4Dao.getInstance(AppManager.context).get(id, Device.class);
            return device;
        } catch (SQLException e) {
            Log.e("jamie", "---getDevices error---" + e.getMessage());
        }
        return null;
    }

    /**
     * 读取设备信息
     *
     * @return
     */
    public List<Device> getDevices() {
        List<Device> list = null;
        try {
            list = M4Dao.getInstance(AppManager.context).getAll(Device.class);
            return list;
        } catch (SQLException e) {
            Log.e("jamie", "---getDevices error---" + e.getMessage());
        }
        return null;
    }
}
